package api

import (
	"api-gateway/basic/config"
	__ "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"api-gateway/handler/resp"
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
	"github.com/share309/gojwt"
	"time"
)

func Login(c *gin.Context) {
	var req request.LoginReq
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 400, "参数有误", err.Error())
		return
	}

	login, err := config.LearnClient.UserLogin(c, &__.UserLoginReq{
		Username: req.Username,
		Password: req.Password,
	})
	if err != nil {
		resp.Error(c, 500, "服务端错误", err.Error())
		return
	}

	token, err := gojwt.NewJWT(config.JWT_KEY).CreateToken(gojwt.CustomClaims{
		ID: uint(login.UserId),
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: time.Now().Unix() + 86400*3,
		},
	})
	if err != nil {
		resp.Error(c, 400, "token生成失败", nil)
		return
	}

	resp.Success(c, token)

}
